#include "includes.h"
#include "sys.h"		    
#include "rs485.h"	 
#include "delay.h"
#include "malloc.h"

RS485_POOL_T g_rs485Pool;

RS485_DMA_POOL_T g_rs485dmaPool;

void RS485_Res_init(void)
{
	u8 i = 0;

	mymemset(&g_rs485Pool, 0, sizeof(RS485_POOL_T));

	for (i = 0; i < 5; i++)
	{
		g_rs485Pool.rs485PoolSub[i].rs485buf = mymalloc(SRAMEX, RS485_BUFF_LEN);
		if (NULL == g_rs485Pool.rs485PoolSub[i].rs485buf)
		{
			printf("RS485_Res_init malloc failed!\r\n");
			goto errExit;
		}
		mymemset(g_rs485Pool.rs485PoolSub[i].rs485buf, 0, RS485_BUFF_LEN);
	}

	return ;

errExit:

	for (i = 0; i < 5; i++)
	{
		if (g_rs485Pool.rs485PoolSub[i].rs485buf)
		{
			myfree(SRAMEX, g_rs485Pool.rs485PoolSub[i].rs485buf);
			g_rs485Pool.rs485PoolSub[i].rs485buf = NULL;
		}
	}

	return ;
}

#if 0
void USART1_IRQHandler(void)
{
	u16 clear = 0;

	OSIntEnter();
	if (RESET != USART_GetITStatus(USART1, USART_IT_IDLE))
	{
		DMA_Cmd(DMA2_Stream2, DISABLE);
		clear = USART1->SR;
		clear = USART1->DR;
		g_rs485Pool.rs485PoolSub[0].rs485RxCnt = RS485_BUFF_LEN - DMA_GetCurrDataCounter(DMA2_Stream2);
		if (0xfa == g_rs485dmaPool.rs485dmabuf1[0] && 0xfb == g_rs485dmaPool.rs485dmabuf1[1] \
			&& 0xfe == g_rs485dmaPool.rs485dmabuf1[g_rs485Pool.rs485PoolSub[0].rs485RxCnt-2] \
			&& 0xfd == g_rs485dmaPool.rs485dmabuf1[g_rs485Pool.rs485PoolSub[0].rs485RxCnt-1])
		{
			mymemcpy(g_rs485Pool.rs485PoolSub[0].rs485buf, g_rs485dmaPool.rs485dmabuf1, g_rs485Pool.rs485PoolSub[0].rs485RxCnt);
		}
		else
		{
			g_rs485Pool.rs485PoolSub[0].rs485RxCnt = 0;
		}
		DMA_ClearFlag(DMA2_Stream2,DMA_FLAG_TCIF2 | DMA_FLAG_FEIF2 | DMA_FLAG_DMEIF2 | DMA_FLAG_TEIF2 | DMA_FLAG_HTIF2);
	    DMA_SetCurrDataCounter(DMA2_Stream2, RS485_BUFF_LEN);
	    DMA_Cmd(DMA2_Stream2, ENABLE);
	}
	OSIntExit();
} 

void USART3_IRQHandler(void)
{
	u16 clear = 0;

	OSIntEnter();
	if (RESET != USART_GetITStatus(USART3, USART_IT_IDLE))
	{
		DMA_Cmd(DMA1_Stream1, DISABLE);
		clear = USART3->SR;
		clear = USART3->DR;
		g_rs485Pool.rs485PoolSub[1].rs485RxCnt = RS485_BUFF_LEN - DMA_GetCurrDataCounter(DMA1_Stream1);
		if (0xfa == g_rs485dmaPool.rs485dmabuf3[0] && 0xfb == g_rs485dmaPool.rs485dmabuf3[1] \
			&& 0xfe == g_rs485dmaPool.rs485dmabuf3[g_rs485Pool.rs485PoolSub[1].rs485RxCnt-2] \
			&& 0xfd == g_rs485dmaPool.rs485dmabuf3[g_rs485Pool.rs485PoolSub[1].rs485RxCnt-1])
		{
			mymemcpy(g_rs485Pool.rs485PoolSub[1].rs485buf, g_rs485dmaPool.rs485dmabuf3, g_rs485Pool.rs485PoolSub[1].rs485RxCnt);
		}
		else
		{
			g_rs485Pool.rs485PoolSub[1].rs485RxCnt = 0;
		}
		DMA_ClearFlag(DMA1_Stream1,DMA_FLAG_TCIF1 | DMA_FLAG_FEIF1 | DMA_FLAG_DMEIF1 | DMA_FLAG_TEIF1 | DMA_FLAG_HTIF1);
	    DMA_SetCurrDataCounter(DMA1_Stream1, RS485_BUFF_LEN);
	    DMA_Cmd(DMA1_Stream1, ENABLE);
	}
	OSIntExit();
}

void UART4_IRQHandler(void)
{
	u16 clear = 0;

	OSIntEnter();
	if (RESET != USART_GetITStatus(UART4, USART_IT_IDLE))
	{
		DMA_Cmd(DMA1_Stream2, DISABLE);
		clear = UART4->SR;
		clear = UART4->DR;
		g_rs485Pool.rs485PoolSub[2].rs485RxCnt = RS485_BUFF_LEN - DMA_GetCurrDataCounter(DMA1_Stream2);
		if (0xfa == g_rs485dmaPool.rs485dmabuf4[0] && 0xfb == g_rs485dmaPool.rs485dmabuf4[1] \
			&& 0xfe == g_rs485dmaPool.rs485dmabuf4[g_rs485Pool.rs485PoolSub[2].rs485RxCnt-2] \
			&& 0xfd == g_rs485dmaPool.rs485dmabuf4[g_rs485Pool.rs485PoolSub[2].rs485RxCnt-1])
		{
			mymemcpy(g_rs485Pool.rs485PoolSub[2].rs485buf, g_rs485dmaPool.rs485dmabuf4, g_rs485Pool.rs485PoolSub[2].rs485RxCnt);
		}
		else
		{
			g_rs485Pool.rs485PoolSub[2].rs485RxCnt = 0;
		}
		DMA_ClearFlag(DMA1_Stream2,DMA_FLAG_TCIF2 | DMA_FLAG_FEIF2 | DMA_FLAG_DMEIF2 | DMA_FLAG_TEIF2 | DMA_FLAG_HTIF2);
	    DMA_SetCurrDataCounter(DMA1_Stream2, RS485_BUFF_LEN);
	    DMA_Cmd(DMA1_Stream2, ENABLE);
	}
	OSIntExit();
}

void UART5_IRQHandler(void)
{
	u16 clear = 0;

	OSIntEnter();
	if (RESET != USART_GetITStatus(UART5, USART_IT_IDLE))
	{
		DMA_Cmd(DMA1_Stream0, DISABLE);
		clear = UART5->SR;
		clear = UART5->DR;
		g_rs485Pool.rs485PoolSub[3].rs485RxCnt = RS485_BUFF_LEN - DMA_GetCurrDataCounter(DMA1_Stream0);
		if (0xfa == g_rs485dmaPool.rs485dmabuf5[0] && 0xfb == g_rs485dmaPool.rs485dmabuf5[1] \
			&& 0xfe == g_rs485dmaPool.rs485dmabuf5[g_rs485Pool.rs485PoolSub[3].rs485RxCnt-2] \
			&& 0xfd == g_rs485dmaPool.rs485dmabuf5[g_rs485Pool.rs485PoolSub[3].rs485RxCnt-1])
		{
			mymemcpy(g_rs485Pool.rs485PoolSub[3].rs485buf, g_rs485dmaPool.rs485dmabuf5, g_rs485Pool.rs485PoolSub[3].rs485RxCnt);
		}
		else
		{
			g_rs485Pool.rs485PoolSub[3].rs485RxCnt = 0;
		}
		DMA_ClearFlag(DMA1_Stream0,DMA_FLAG_TCIF0 | DMA_FLAG_FEIF0 | DMA_FLAG_DMEIF0 | DMA_FLAG_TEIF0 | DMA_FLAG_HTIF0);
	    DMA_SetCurrDataCounter(DMA1_Stream0, RS485_BUFF_LEN);
	    DMA_Cmd(DMA1_Stream0, ENABLE);
	}
	OSIntExit();
}

void USART6_IRQHandler(void)
{
	u16 clear = 0;

	OSIntEnter();
	if (RESET != USART_GetITStatus(USART6, USART_IT_IDLE))
	{
		DMA_Cmd(DMA2_Stream1, DISABLE);
		clear = USART6->SR;
		clear = USART6->DR;
		g_rs485Pool.rs485PoolSub[4].rs485RxCnt = RS485_BUFF_LEN - DMA_GetCurrDataCounter(DMA2_Stream1);
		if (0xfa == g_rs485dmaPool.rs485dmabuf6[0] && 0xfb == g_rs485dmaPool.rs485dmabuf6[1] \
			&& 0xfe == g_rs485dmaPool.rs485dmabuf6[g_rs485Pool.rs485PoolSub[4].rs485RxCnt-2] \
			&& 0xfd == g_rs485dmaPool.rs485dmabuf6[g_rs485Pool.rs485PoolSub[4].rs485RxCnt-1])
		{
			mymemcpy(g_rs485Pool.rs485PoolSub[4].rs485buf, g_rs485dmaPool.rs485dmabuf6, g_rs485Pool.rs485PoolSub[4].rs485RxCnt);
		}
		else
		{
			g_rs485Pool.rs485PoolSub[4].rs485RxCnt = 0;
		}
		DMA_ClearFlag(DMA2_Stream1,DMA_FLAG_TCIF1 | DMA_FLAG_FEIF1 | DMA_FLAG_DMEIF1 | DMA_FLAG_TEIF1 | DMA_FLAG_HTIF1);
	    DMA_SetCurrDataCounter(DMA2_Stream1, RS485_BUFF_LEN);
	    DMA_Cmd(DMA2_Stream1, ENABLE);
	}
	OSIntExit();
} 

void rs485_1_init(u32 bound)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;

	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);

	GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1);
	GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1);

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_Init(GPIOA, &GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_Init(GPIOB, &GPIO_InitStructure);

	USART_InitStructure.USART_BaudRate = bound;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_Parity = USART_Parity_No;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
	USART_Init(USART1, &USART_InitStructure);

	USART_Cmd(USART1, ENABLE);

	USART_ClearFlag(USART1, USART_FLAG_TC);

	USART_ITConfig(USART1, USART_IT_IDLE, ENABLE);

	NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);

	USART_DMACmd(USART1, USART_DMAReq_Rx, ENABLE);

	RS485_1_TX_EN=0;
}

void rs485_3_init(u32 bound)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;

	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);

	GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_USART3);
	GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_USART3);

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_Init(GPIOB, &GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_Init(GPIOA, &GPIO_InitStructure);

	USART_InitStructure.USART_BaudRate = bound;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_Parity = USART_Parity_No;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
	USART_Init(USART3, &USART_InitStructure);

	USART_Cmd(USART3, ENABLE);

	USART_ClearFlag(USART3, USART_FLAG_TC);

	USART_ITConfig(USART3, USART_IT_IDLE, ENABLE);

	NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);

	USART_DMACmd(USART3, USART_DMAReq_Rx, ENABLE);

	RS485_3_TX_EN = 0;
}

void rs485_4_init(u32 bound)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;

	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);

	GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_UART4);
	GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_UART4);

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_Init(GPIOC, &GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_Init(GPIOA, &GPIO_InitStructure);

	USART_InitStructure.USART_BaudRate = bound;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_Parity = USART_Parity_No;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
	USART_Init(UART4, &USART_InitStructure);

	USART_Cmd(UART4, ENABLE);

	USART_ClearFlag(UART4, USART_FLAG_TC);

	USART_ITConfig(UART4, USART_IT_IDLE, ENABLE);

	NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority =3;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);

	USART_DMACmd(UART4, USART_DMAReq_Rx, ENABLE);

	RS485_4_TX_EN=0;
}

void rs485_5_init(u32 bound)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;

	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE);

	GPIO_PinAFConfig(GPIOC,GPIO_PinSource12, GPIO_AF_UART5);
	GPIO_PinAFConfig(GPIOD,GPIO_PinSource2, GPIO_AF_UART5);

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_Init(GPIOC, &GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_Init(GPIOD, &GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_Init(GPIOD, &GPIO_InitStructure);

	USART_InitStructure.USART_BaudRate = bound;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_Parity = USART_Parity_No;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
	USART_Init(UART5, &USART_InitStructure);

	USART_Cmd(UART5, ENABLE);

	USART_ClearFlag(UART5, USART_FLAG_TC);

	USART_ITConfig(UART5, USART_IT_IDLE, ENABLE);

	NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority =3;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);

	USART_DMACmd(UART5, USART_DMAReq_Rx, ENABLE);

	RS485_5_TX_EN=0;
}

void rs485_6_init(u32 bound)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;

	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE);

	GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_USART6);
	GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_USART6);

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_Init(GPIOC, &GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_Init(GPIOC, &GPIO_InitStructure);

	USART_InitStructure.USART_BaudRate = bound;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_Parity = USART_Parity_No;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
	USART_Init(USART6, &USART_InitStructure);

	USART_Cmd(USART6, ENABLE);

	USART_ClearFlag(USART6, USART_FLAG_TC);

	USART_ITConfig(USART6, USART_IT_IDLE, ENABLE);

	NVIC_InitStructure.NVIC_IRQChannel = USART6_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);

	USART_DMACmd(USART6, USART_DMAReq_Rx, ENABLE);

	RS485_6_TX_EN = 0;
}

void RS485_Init(u32 bound)
{
	rs485_1_init(bound);
	rs485_3_init(bound);
	rs485_4_init(bound);
	rs485_5_init(bound);
	rs485_6_init(bound);
}

void rs485_dma_1_init(void)
{
	DMA_InitTypeDef  DMA_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2,ENABLE);

	DMA_DeInit(DMA2_Stream2);
	
	while (DMA_GetCmdStatus(DMA2_Stream2) != DISABLE)
	{}

	DMA_InitStructure.DMA_Channel = DMA_Channel_4;
	DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&USART1->DR;
	DMA_InitStructure.DMA_Memory0BaseAddr = (u32)g_rs485dmaPool.rs485dmabuf1;
	DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
	DMA_InitStructure.DMA_BufferSize = RS485_BUFF_LEN;
	DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
	DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
	DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
	DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
	DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
	DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
	DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;         
	DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
	DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
	DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
	DMA_Init(DMA2_Stream2, &DMA_InitStructure);

	NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream2_IRQn;  
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;  
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;  
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;  
	NVIC_Init(&NVIC_InitStructure);  

	DMA_Cmd(DMA2_Stream2, ENABLE);
}

void rs485_dma_3_init(void)
{
	DMA_InitTypeDef  DMA_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1,ENABLE);

	DMA_DeInit(DMA1_Stream1);
	
	while (DMA_GetCmdStatus(DMA1_Stream1) != DISABLE)
	{}

	DMA_InitStructure.DMA_Channel = DMA_Channel_4;
	DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&USART3->DR;
	DMA_InitStructure.DMA_Memory0BaseAddr = (u32)g_rs485dmaPool.rs485dmabuf3;
	DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
	DMA_InitStructure.DMA_BufferSize = RS485_BUFF_LEN;
	DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
	DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
	DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
	DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
	DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
	DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
	DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;         
	DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
	DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
	DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
	DMA_Init(DMA1_Stream1, &DMA_InitStructure);

	NVIC_InitStructure.NVIC_IRQChannel = DMA1_Stream1_IRQn;  
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;  
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;  
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;  
	NVIC_Init(&NVIC_InitStructure);  

	DMA_Cmd(DMA1_Stream1, ENABLE);
}

void rs485_dma_4_init(void)
{
	DMA_InitTypeDef  DMA_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1,ENABLE);

	DMA_DeInit(DMA1_Stream2);
	
	while (DMA_GetCmdStatus(DMA1_Stream2) != DISABLE)
	{}

	DMA_InitStructure.DMA_Channel = DMA_Channel_4;
	DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&UART4->DR;
	DMA_InitStructure.DMA_Memory0BaseAddr = (u32)g_rs485dmaPool.rs485dmabuf4;
	DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
	DMA_InitStructure.DMA_BufferSize = RS485_BUFF_LEN;
	DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
	DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
	DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
	DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
	DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
	DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
	DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;         
	DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
	DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
	DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
	DMA_Init(DMA1_Stream2, &DMA_InitStructure);

	NVIC_InitStructure.NVIC_IRQChannel = DMA1_Stream2_IRQn;  
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;  
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;  
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;  
	NVIC_Init(&NVIC_InitStructure);  

	DMA_Cmd(DMA1_Stream2, ENABLE);
}

void rs485_dma_5_init(void)
{
	DMA_InitTypeDef  DMA_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1,ENABLE);

	DMA_DeInit(DMA1_Stream0);
	
	while (DMA_GetCmdStatus(DMA1_Stream0) != DISABLE)
	{}

	DMA_InitStructure.DMA_Channel = DMA_Channel_4;
	DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&UART5->DR;
	DMA_InitStructure.DMA_Memory0BaseAddr = (u32)g_rs485dmaPool.rs485dmabuf5;
	DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
	DMA_InitStructure.DMA_BufferSize = RS485_BUFF_LEN;
	DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
	DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
	DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
	DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
	DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
	DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
	DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;         
	DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
	DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
	DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
	DMA_Init(DMA1_Stream0, &DMA_InitStructure);

	NVIC_InitStructure.NVIC_IRQChannel = DMA1_Stream0_IRQn;  
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;  
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;  
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;  
	NVIC_Init(&NVIC_InitStructure);  

	DMA_Cmd(DMA1_Stream0, ENABLE);
}

void rs485_dma_6_init(void)
{
	DMA_InitTypeDef  DMA_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2,ENABLE);

	DMA_DeInit(DMA2_Stream1);
	
	while (DMA_GetCmdStatus(DMA2_Stream1) != DISABLE)
	{}

	DMA_InitStructure.DMA_Channel = DMA_Channel_5;
	DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&USART6->DR;
	DMA_InitStructure.DMA_Memory0BaseAddr = (u32)g_rs485dmaPool.rs485dmabuf6;
	DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
	DMA_InitStructure.DMA_BufferSize = RS485_BUFF_LEN;
	DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
	DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
	DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
	DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
	DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
	DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
	DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;         
	DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
	DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
	DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
	DMA_Init(DMA2_Stream1, &DMA_InitStructure);

	NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream1_IRQn;  
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;  
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;  
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;  
	NVIC_Init(&NVIC_InitStructure);  

	DMA_Cmd(DMA2_Stream1, ENABLE);
}

void Uart_Dma_Res_Init(void)
{
	mymemset(&g_rs485dmaPool, 0, sizeof(RS485_DMA_POOL_T));

	g_rs485dmaPool.rs485dmabuf1 = mymalloc(SRAMEX, RS485_BUFF_LEN);
	if (NULL == g_rs485dmaPool.rs485dmabuf1)
	{
		goto errExit;
	}
	mymemset(g_rs485dmaPool.rs485dmabuf1, 0, RS485_BUFF_LEN);

	g_rs485dmaPool.rs485dmabuf3 = mymalloc(SRAMEX, RS485_BUFF_LEN);
	if (NULL == g_rs485dmaPool.rs485dmabuf3)
	{
		goto errExit;
	}
	mymemset(g_rs485dmaPool.rs485dmabuf3, 0, RS485_BUFF_LEN);

	g_rs485dmaPool.rs485dmabuf4 = mymalloc(SRAMEX, RS485_BUFF_LEN);
	if (NULL == g_rs485dmaPool.rs485dmabuf4)
	{
		goto errExit;
	}
	mymemset(g_rs485dmaPool.rs485dmabuf4, 0, RS485_BUFF_LEN);

	g_rs485dmaPool.rs485dmabuf5 = mymalloc(SRAMEX, RS485_BUFF_LEN);
	if (NULL == g_rs485dmaPool.rs485dmabuf5)
	{
		goto errExit;
	}
	mymemset(g_rs485dmaPool.rs485dmabuf5, 0, RS485_BUFF_LEN);

	g_rs485dmaPool.rs485dmabuf6 = mymalloc(SRAMEX, RS485_BUFF_LEN);
	if (NULL == g_rs485dmaPool.rs485dmabuf6)
	{
		goto errExit;
	}
	mymemset(g_rs485dmaPool.rs485dmabuf6, 0, RS485_BUFF_LEN);

	return ;

errExit:

	if (g_rs485dmaPool.rs485dmabuf6)
	{
		myfree(SRAMEX, g_rs485dmaPool.rs485dmabuf6);
		g_rs485dmaPool.rs485dmabuf6 = NULL;
	}

	if (g_rs485dmaPool.rs485dmabuf5)
	{
		myfree(SRAMEX, g_rs485dmaPool.rs485dmabuf5);
		g_rs485dmaPool.rs485dmabuf5 = NULL;
	}

	if (g_rs485dmaPool.rs485dmabuf4)
	{
		myfree(SRAMEX, g_rs485dmaPool.rs485dmabuf4);
		g_rs485dmaPool.rs485dmabuf4 = NULL;
	}
	
	if (g_rs485dmaPool.rs485dmabuf3)
	{
		myfree(SRAMEX, g_rs485dmaPool.rs485dmabuf3);
		g_rs485dmaPool.rs485dmabuf3 = NULL;
	}

	if (g_rs485dmaPool.rs485dmabuf1)
	{
		myfree(SRAMEX, g_rs485dmaPool.rs485dmabuf1);
		g_rs485dmaPool.rs485dmabuf1 = NULL;
	}

	return ;
}

void Uart_DMA_Init(void)
{
	Uart_Dma_Res_Init();

	rs485_dma_1_init();
	rs485_dma_3_init();
	rs485_dma_4_init();
	rs485_dma_5_init();
	rs485_dma_6_init();
}

void RS485_1_Send_Data(u8 *buf,u16 len)
{
	u8 t = 0;

	RS485_1_TX_EN = 1;
  	for(t = 0; t < len; t++)
	{
		while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
		USART_SendData(USART1, buf[t]);
	}
	while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
	g_rs485Pool.rs485PoolSub[0].rs485RxCnt = 0;
	RS485_1_TX_EN = 0;
}

void RS485_3_Send_Data(u8 *buf, u16 len)
{
	u8 t = 0;

	RS485_3_TX_EN=1;
  	for (t = 0; t < len; t++)
	{
		while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);
		USART_SendData(USART3, buf[t]);
	}
	while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);
	g_rs485Pool.rs485PoolSub[1].rs485RxCnt = 0;
	RS485_3_TX_EN = 0;
}

void RS485_4_Send_Data(u8 *buf, u16 len)
{
	u8 t = 0;

	RS485_4_TX_EN=1;
  	for (t = 0; t < len; t++)
	{
		while (USART_GetFlagStatus(UART4, USART_FLAG_TC) == RESET);
		USART_SendData(UART4, buf[t]);
	}
	while (USART_GetFlagStatus(UART4, USART_FLAG_TC) == RESET);
	g_rs485Pool.rs485PoolSub[2].rs485RxCnt = 0;
	RS485_4_TX_EN = 0;
}

void RS485_5_Send_Data(u8 *buf,u16 len)
{
	u8 t = 0;

	RS485_5_TX_EN = 1;
  	for (t = 0; t < len; t++)
	{
		while(USART_GetFlagStatus(UART5, USART_FLAG_TC)==RESET);
		USART_SendData(UART5, buf[t]);
	}
	while(USART_GetFlagStatus(UART5, USART_FLAG_TC)==RESET);
	g_rs485Pool.rs485PoolSub[3].rs485RxCnt = 0;
	RS485_5_TX_EN=0;
}

void RS485_6_Send_Data(u8 *buf,u16 len)
{
	u8 t = 0;

	RS485_6_TX_EN=1;
  	for (t = 0; t < len; t++)
	{
		while(USART_GetFlagStatus(USART6, USART_FLAG_TC)==RESET);
		USART_SendData(USART6, buf[t]);
	}
	while(USART_GetFlagStatus(USART6, USART_FLAG_TC)==RESET);
	g_rs485Pool.rs485PoolSub[4].rs485RxCnt = 0;
	RS485_6_TX_EN=0;
}

void RS485_1_Receive_Data(u8 *buf,u16 *len)
{
	u16 rxlen = 0;

	if (g_rs485Pool.rs485PoolSub[0].rs485RxCnt >= 10 && 0xfa == g_rs485Pool.rs485PoolSub[0].rs485buf[0] \
		&& 0xfb == g_rs485Pool.rs485PoolSub[0].rs485buf[1] \
		&& 0xfe == g_rs485Pool.rs485PoolSub[0].rs485buf[g_rs485Pool.rs485PoolSub[0].rs485RxCnt-2] \
		&& 0xfd == g_rs485Pool.rs485PoolSub[0].rs485buf[g_rs485Pool.rs485PoolSub[0].rs485RxCnt-1])
	{
		rxlen = g_rs485Pool.rs485PoolSub[0].rs485RxCnt;
		*len = rxlen;
		mymemcpy(buf, g_rs485Pool.rs485PoolSub[0].rs485buf, rxlen);
		g_rs485Pool.rs485PoolSub[0].rs485RxCnt = 0;
	}
}

void RS485_3_Receive_Data(u8 *buf,u16 *len)
{
	u16 rxlen = 0;

	if (g_rs485Pool.rs485PoolSub[1].rs485RxCnt >= 10 && 0xfa == g_rs485Pool.rs485PoolSub[1].rs485buf[0] \
		&& 0xfb == g_rs485Pool.rs485PoolSub[1].rs485buf[1] \
		&& 0xfe == g_rs485Pool.rs485PoolSub[1].rs485buf[g_rs485Pool.rs485PoolSub[1].rs485RxCnt-2] \
		&& 0xfd == g_rs485Pool.rs485PoolSub[1].rs485buf[g_rs485Pool.rs485PoolSub[1].rs485RxCnt-1])
	{
		rxlen = g_rs485Pool.rs485PoolSub[1].rs485RxCnt;
		*len = rxlen;
		mymemcpy(buf, g_rs485Pool.rs485PoolSub[1].rs485buf, rxlen);
		g_rs485Pool.rs485PoolSub[1].rs485RxCnt = 0;
	}
}

void RS485_4_Receive_Data(u8 *buf,u16 *len)
{
	u16 rxlen = 0;

	if (g_rs485Pool.rs485PoolSub[2].rs485RxCnt >= 10 && 0xfa == g_rs485Pool.rs485PoolSub[2].rs485buf[0] \
		&& 0xfb == g_rs485Pool.rs485PoolSub[2].rs485buf[1] \
		&& 0xfe == g_rs485Pool.rs485PoolSub[2].rs485buf[g_rs485Pool.rs485PoolSub[2].rs485RxCnt-2] \
		&& 0xfd == g_rs485Pool.rs485PoolSub[2].rs485buf[g_rs485Pool.rs485PoolSub[2].rs485RxCnt-1])
	{
		rxlen = g_rs485Pool.rs485PoolSub[2].rs485RxCnt;
		*len = rxlen;
		mymemcpy(buf, g_rs485Pool.rs485PoolSub[2].rs485buf, rxlen);
		g_rs485Pool.rs485PoolSub[2].rs485RxCnt = 0;
	}
}

void RS485_5_Receive_Data(u8 *buf, u16 *len)
{
	u16 rxlen = 0;

	if (g_rs485Pool.rs485PoolSub[3].rs485RxCnt >= 10 && 0xfa == g_rs485Pool.rs485PoolSub[3].rs485buf[0] \
		&& 0xfb == g_rs485Pool.rs485PoolSub[3].rs485buf[1] \
		&& 0xfe == g_rs485Pool.rs485PoolSub[3].rs485buf[g_rs485Pool.rs485PoolSub[3].rs485RxCnt-2] \
		&& 0xfd == g_rs485Pool.rs485PoolSub[3].rs485buf[g_rs485Pool.rs485PoolSub[3].rs485RxCnt-1])
	{
		rxlen = g_rs485Pool.rs485PoolSub[3].rs485RxCnt;
		*len = rxlen;
		mymemcpy(buf, g_rs485Pool.rs485PoolSub[3].rs485buf, rxlen);
		g_rs485Pool.rs485PoolSub[3].rs485RxCnt = 0;
	}
}

void RS485_6_Receive_Data(u8 *buf, u16 *len)
{
	u16 rxlen = 0;

	if (g_rs485Pool.rs485PoolSub[4].rs485RxCnt >= 10 && 0xfa == g_rs485Pool.rs485PoolSub[4].rs485buf[0] \
		&& 0xfb == g_rs485Pool.rs485PoolSub[4].rs485buf[1] \
		&& 0xfe == g_rs485Pool.rs485PoolSub[4].rs485buf[g_rs485Pool.rs485PoolSub[4].rs485RxCnt-2] \
		&& 0xfd == g_rs485Pool.rs485PoolSub[4].rs485buf[g_rs485Pool.rs485PoolSub[4].rs485RxCnt-1])
	{
		rxlen = g_rs485Pool.rs485PoolSub[4].rs485RxCnt;
		*len = rxlen;
		mymemcpy(buf, g_rs485Pool.rs485PoolSub[4].rs485buf, rxlen);
		g_rs485Pool.rs485PoolSub[4].rs485RxCnt = 0;
	}
}
#endif

